<template>
  <div class="search-box">
    <!-- 位置信息 -->
    <div class="location">
      <div class="city" @click="cityClick">{{ currentCity.cityName }}</div>
      <div class="position" @click="positionClick">
        <span class="text">我的位置</span>
        <img src="@/assets/img/home/icon_location.png" alt="" />
      </div>
    </div>
    <!-- 日期范围 -->
    <div class="date-range" @click="showCalendar = true">
      <div class="start">
        <span class="tip">入住</span>
        <span class="time">{{ startDateStr }}</span>
      </div>
      <div class="stay">共{{ stayCount }}晚</div>
      <div class="end">
        <span class="tip">离店</span>
        <span class="time">{{ endDateStr }}</span>
      </div>
    </div>
    <van-calendar
      class="search-box"
      v-model:show="showCalendar"
      @confirm="onConfirm"
      type="range"
      color="#ff9854"
      :round="false"
      :show-confirm="false"
    />
    <!-- 价格/人数的选择 -->
    <div class="price-counter">
      <div class="start">价格不限</div>
      <div class="end">人数不限</div>
    </div>
    <!-- 关键字 -->
    <div class="keyword">关键字/位置/民俗名</div>
    <!-- 热门建议 -->
    <div class="hot-suggest">
      <template v-for="(item, index) in hotSuggests" :key="index">
        <div
          class="item"
          :style="{
            color: item.tagText.color,
            background: item.tagText.background.color,
          }"
        >
          {{ item.tagText.text }}
        </div>
      </template>
      <!-- 搜索按钮 -->
    </div>
    <div class="search-btn">
      <div class="btn" @click="searchBtnClick">开始搜索</div>
    </div>
  </div>
</template>

<script setup>
import { computed, ref } from "vue";
import { storeToRefs } from "pinia";
import useCityStore from "@/store/modules/city";
import { formatMonthDay, getDiffDays } from "@/utils/format_date";
import { useRouter } from "vue-router";
import useHomeStore from "@/store/modules/home";
import useMainStore from "@/store/modules/main";

// 选择城市
const router = useRouter();
const cityClick = () => {
  router.push("/city");
};

//获取位置
const positionClick = () => {
  navigator.geolocation.getCurrentPosition(
    (res) => {
      console.log("获取位置成功", res);
    },
    (err) => {
      console.log("获取位置失败", err);
    },
    {
      enableHighAccuracy: true,
      timeout: 5000,
      maximumAge: 0,
    }
  );
};

// 获取当前的城市
const cityStore = useCityStore();
const { currentCity } = cityStore;

// 日期范围的处理
const mainStore = useMainStore();
const { startDate, endDate } = storeToRefs(mainStore);

const startDateStr = computed(() => formatMonthDay(startDate.value));
const endDateStr = computed(() => formatMonthDay(endDate.value));
const stayCount = ref(getDiffDays(startDate.value, endDate.value));

const showCalendar = ref(false);
const onConfirm = (value) => {
  // 1.设置日期
  const selectStartDate = value[0];
  const selectEndDate = value[1];
  mainStore.startDate = selectStartDate;
  mainStore.endDate = selectEndDate;
  stayCount.value = getDiffDays(selectStartDate, selectEndDate);

  // 2.隐藏日历
  showCalendar.value = false;
};

// 热门建议
const homeStore = useHomeStore();
const { hotSuggests } = storeToRefs(homeStore);

//开始搜索
const searchBtnClick = () => {
  router.push({
    path: "/search",
    query: {
      startDate: startDate.value,
      endDate: endDate.value,
      currentCity: currentCity.cityName,
    },
  });
};
</script>

<style lang="less" scoped>
.location {
  display: flex;
  align-items: center;
  height: 44px;
  padding: 0 20px;

  .city {
    color: #333;
    flex: 1;
  }

  .position {
    display: flex;
    align-items: center;
    width: 74px;

    .text {
      position: relative;
      top: 2px;
      font-size: 12px;
      color: #666;
    }

    img {
      margin-left: 5px;
      width: 18px;
      height: 18px;
    }
  }
}

.date-range {
  height: 44px;
  display: flex;
  align-items: center;
  padding: 0 20px;
  color: #999;

  .start {
    flex: 1.2;
    display: flex;
    flex-direction: column;

    .tip {
      font-size: 12px;
    }

    .time {
      margin-top: 3px;
      font-size: 16px;
      color: #000;
    }
  }

  .stay {
    flex: 1;
    color: #666;
  }

  .end {
    flex: 1;
    display: flex;
    flex-direction: column;

    .tip {
      font-size: 12px;
    }

    .time {
      margin-top: 3px;
      font-size: 16px;
      color: #000;
    }
  }
}

.search-box {
  --van-calendar-popup-height: 100%;
}

.price-counter {
  color: #666;
  padding: 0 20px;
  height: 44px;
  display: flex;
  align-items: center;

  .start {
    flex: 2.2;
  }

  .end {
    flex: 1;
  }
}

.keyword {
  color: #666;
  padding: 0 20px;
  height: 44px;
  display: flex;
  align-items: center;
}

.hot-suggest {
  padding: 0 20px;
  height: auto;
  display: flex;
  flex-wrap: wrap;

  .item {
    padding: 4px 8px;
    margin: 4px;
    border-radius: 14px;
    font-size: 12px;
    line-height: 1;
  }
}

.search-btn {
  margin-top: 10px;
  padding: 0 20px;

  .btn {
    width: 100%;
    height: 38px;
    max-height: 50px;
    font-weight: 500;
    font-size: 18px;
    line-height: 38px;
    text-align: center;
    border-radius: 20px;
    color: #fff;
    background-image: var(--theme-linear-gradient);
  }
}
</style>
